package com.mytaobao.main.cart;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baseproject.eventbus.EventBus;
import com.baseproject.itemdecoration.DividerLinearItemDecoration;
import com.baseproject.util.LogUtils;
import com.mytaobao.R;
import com.mytaobao.TaobaoApplication;
import com.mytaobao.main.cart.func.cart.ItemCommodity;
import com.mytaobao.main.cart.func.cart.SelectedChangeEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2017/3/17.
 */

public class CartRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<List<ItemCommodity>> mCommodities = new ArrayList<>();
    RecyclerView mRecyclerView;

    public int getCountAll() {
        int result = 0;
        for (int i = 0; i < mCommodities.size(); i++) {
            List<ItemCommodity> item = mCommodities.get(i);
            for (int j = 0; j < item.size(); j++ ) {
                result++;
            }
        }
        return result;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecyclerView = recyclerView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_cart_item, parent, false);
        CartItemViewHolder holder = new CartItemViewHolder(itemView);
        holder.rv.setLayoutManager(new LinearLayoutManager(TaobaoApplication.getApplication(),LinearLayoutManager.VERTICAL, false));
        CartRecyclerViewSubAdapter subAdapter = new CartRecyclerViewSubAdapter(this);
        holder.rv.setAdapter(subAdapter);
        //添加分割线
        holder.rv.addItemDecoration(new DividerLinearItemDecoration(holder.rv.getContext(), DividerLinearItemDecoration.VERTICAL, R.drawable.divider_bg_white));
        return holder;
    }

    public final class CartItemViewHolder extends RecyclerView.ViewHolder {
        RecyclerView rv;
        public CartItemViewHolder(View itemView) {
            super(itemView);
            rv = (RecyclerView) itemView.findViewById(R.id.rv_cart_subitem);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LogUtils.d("[onBindViewHolder] this = " + this + " positon = " + position);
        List<ItemCommodity> curCommodities = mCommodities.get(position);
        CartRecyclerViewSubAdapter myCartRecyclerViewSubAdapter = (CartRecyclerViewSubAdapter)((CartItemViewHolder)holder).rv.getAdapter();
        myCartRecyclerViewSubAdapter.setPosition(position);
        myCartRecyclerViewSubAdapter.setCommodities(mCommodities.get(position));
    }

    @Override
    public int getItemCount() {
        return mCommodities.size();
    }
    public void removeItem(int position) {
        notifyItemRemoved(position);
        mCommodities.remove(position);
    }

    public void setCommodities(List<List<ItemCommodity>> commodities) {
         this.mCommodities = commodities;
         EventBus.getDefault().post(new SelectedChangeEvent(0, 0));
//  如果直接从cache中拿数据，由于速度快，这个调用getItemCount时已经有size了，不需要再次调用notifyDataSetChanged，
//  反之如果从数据库拿数据，由于慢，系统getItemCount时返回的是0，如果不notify，因此不会渲染rv,
//         notifyDataSetChanged();
    }

    public void updateAllSelected(boolean isSelected) {
        for (List<ItemCommodity> itemCommodity : mCommodities) {
            for (ItemCommodity item : itemCommodity) {
                item.setChildSelected(isSelected);
            }
        }
    }

    public SelectedChangeEvent getSelectedChangeEvent() {
        int count = 0;
        float price = 0;
        for (List<ItemCommodity> itemCommodities : mCommodities) {
            for (ItemCommodity itemCommodity : itemCommodities) {
                if (itemCommodity.isChildSelected()) {
                    count++;
                    price += itemCommodity.getPriceNow() * itemCommodity.getCountInCart();
                }
            }
        }
        return new SelectedChangeEvent(price, count);
    }

    public int getAllSubItemCount() {
        int count = 0;
        for (List<ItemCommodity> itemCommodities : mCommodities) {
            for (ItemCommodity itemCommodity : itemCommodities) {
                    count++;
            }
        }
        return count;
    }
}
